package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
	"mime/multipart"
	"wskyygyso/api/app/requests/validators"
	"wskyygyso/api/pkg/auth"
)

type UserRequest struct {
}

func UserSave(data interface{}, c *gin.Context) map[string][]string {
	return nil
}

type UserUpdateProfileRequest struct {
	Name         string `json:"name" valid:"name"`
	City         string `json:"city" valid:"city"`
	Introduction string `json:"introduction" valid:"introduction"`
}

func UserUpdateSave(data interface{}, c *gin.Context) map[string][]string {
	uid := auth.CurrentUID(c)
	rules := govalidator.MapData{
		"name": []string{
			"required", "alpha_num", "between:3,20", "not_exists:users,name," + uid,
		},
		"introduction": []string{"min_cn:4", "max_cn:240"},
		"city":         []string{"min_cn:2", "max_cn:20"},
	}
	messages := govalidator.MapData{
		"name": []string{
			"required:用户名为必填项",
			"alpha_num:用户名格式错误，只允许数字和英文",
			"between:用户名长度需在 3~20 之间",
			"not_exists:用户名已被占用",
		},
		"introduction": []string{
			"min_cn:描述长度需至少 4 个字",
			"max_cn:描述长度不能超过 240 个字",
		},
		"city": []string{
			"min_cn:城市需至少 2 个字",
			"max_cn:城市不能超过 20 个字",
		},
	}
	return validate(data, rules, messages)
}

type UserUpdateEmailRequest struct {
	Email      string `json:"email" valid:"email"`
	VerifyCode string `json:"verify_code" valid:"verify_code"`
}

func UserUpdateEmailSave(data interface{}, c *gin.Context) map[string][]string {
	currentUser := auth.CurrentUser(c)
	rules := govalidator.MapData{
		"email": []string{
			"required", "email", "not_in:" + currentUser.Email, "not_exists:users,email," + currentUser.GetStringId(), "max:20", "min:5",
		},
		"verify_code": []string{
			"required", "digits:6",
		},
	}
	messages := govalidator.MapData{
		"email": []string{
			"required:Email 为必填项",
			"min:Email 长度需大于 4",
			"max:Email 长度需小于 30",
			"email:Email 格式不正确，请提供有效的邮箱地址",
			"not_exists:Email 已被占用",
			"not_in:新的 Email 与老 Email 一致",
		},
		"verify_code": []string{
			"required:验证码答案必填",
			"digits:验证码长度必须为 6 位的数字",
		},
	}
	errs := validate(data, rules, messages)
	request := data.(*UserUpdateEmailRequest)
	errs = validators.ValidateVerifyCode(request.Email, request.VerifyCode, errs)
	return errs
}

type UserUpdatePhoneRequest struct {
	Phone      string `json:"phone" valid:"phone"`
	VerifyCode string `json:"verify_code" valid:"verify_code"`
}

func UserUpdatePhoneSave(data interface{}, c *gin.Context) map[string][]string {
	currentUser := auth.CurrentUser(c)
	rules := govalidator.MapData{
		"phone": []string{
			"required", "digits:11", "not_in:" + currentUser.Phone, "not_exists:users,phone," + currentUser.GetStringId(),
		},
		"verify_code": []string{
			"required", "digits:6",
		},
	}
	messages := govalidator.MapData{
		"phone": []string{
			"required:手机号为必填项，参数名称 phone",
			"digits:手机号长度必须为 11 位的数字",
			"not_exists:手机号已被占用",
			"not_in:新的手机与老手机号一致",
		},
		"verify_code": []string{
			"required:验证码答案必填",
			"digits:验证码长度必须为 6 位的数字",
		},
	}

	errs := validate(data, rules, messages)
	_data := data.(*UserUpdatePhoneRequest)
	errs = validators.ValidateVerifyCode(_data.Phone, _data.VerifyCode, errs)
	return errs
}

type UserUpdatePasswordRequest struct {
	PassWord           string `json:"pass_word" valid:"pass_word"`
	NewPassWord        string `json:"new_pass_word" valid:"new_pass_word"`
	NewPassWordConfirm string `json:"new_pass_word_confirm" valid:"new_pass_word_confirm"`
}

func UserUpdatePasswordSave(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"pass_word": []string{
			"required", "min:6",
		},
		"new_pass_word": []string{
			"required", "min:6",
		},
		"new_pass_word_confirm": []string{
			"required", "min:6",
		},
	}
	messages := govalidator.MapData{
		"pass_word": []string{
			"required:必须设置密码",
			"min:密码最小长度6",
		},
		"new_pass_word": []string{
			"required:必须设置新密码",
			"min:新密码最小长度6",
		},
		"new_pass_word_confirm": []string{
			"required:必须设置新密码确认",
			"min:新确认密码最小长度6",
		},
	}
	errs := validate(data, rules, messages)
	_data := data.(*UserUpdatePasswordRequest)
	validators.ValidatePasswordConfirm(_data.NewPassWord, _data.NewPassWordConfirm, errs)
	return errs
}

type UserUpdateAvatarRequest struct {
	Avatar *multipart.FileHeader `form:"avatar" valid:"avatar"`
}

func UserUpdateAvatarSave(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"file:avatar": []string{
			"required", "ext:jpg,png,jpeg", "size:20971520",
		},
	}
	messages := govalidator.MapData{
		"file:avatar": []string{
			"ext:ext头像只能上传 png, jpg, jpeg 任意一种的图片",
			"size:头像文件最大不能超过 20MB",
			"required:必须上传图片",
		},
	}
	return validateFile(c, data, rules, messages)
}
